package com.cedifie.lendapp.ui.user.impl

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.cedifie.lendapp.MyApplication
import com.cedifie.lendapp.R
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.mvp.presenter.MvpPresenter
import com.cedifie.lendapp.ui.user.view.ICedUserView
import com.cedifie.lendapp.utils.event.CedEventType
import com.cedifie.lendapp.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/14 16:26
 * @Annotation：个人中心 Presenter 实现
 */
class CedUserPresenterImpl(context: Context?) : MvpPresenter<ICedUserView>(context) {

    private var userModel: CedUserModelImpl? = null
    init {
        userModel = CedUserModelImpl()
    }

    /**
     * 获取客服联系方式列表
     */
    fun loadCedContactListAction(){
        val commonString = SharedHelper.getCedSPString(SharedHelper.CED_SPKEY.CEDIFIE_COMMON)
        if (commonString.isNotEmpty()){
            val data = JSONObject(commonString)
            val contactList = arrayListOf<JSONObject>()
            val customize = data.optJSONObject("customize")
            if (customize != null){
                val workTime = customize.optString("workTime","")
                mvpView?.showCedCustomerWorkTime(workTime)
                val mobileList = customize.optString("mobile").split(",")
                for (i in mobileList.indices){
                    val obj = JSONObject()
                    obj.put("type","MOBILE")
                    obj.put("value", mobileList[i])
                    contactList.add(obj)
                }
                val emailList = customize.optString("email").split(",")
                for (i in emailList.indices){
                    val obj = JSONObject()
                    obj.put("type","EMAIL")
                    obj.put("value", emailList[i])
                    contactList.add(obj)
                }
                mvpView?.showCedContactList(contactList)
            }
        }
    }

    /**
     * 打分
     */
    fun loadCedRateUsAction(){
        userModel?.loadCedRateUsAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")){
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null) {
                            mvpView?.showCedRateUsDialog(obj)
                        }
                    }
                    else -> {//失败
                        mvpView?.showCedToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideCedLoading()
            }
        })
    }

    /**
     * 复制
     */
    fun onCedCopyAction(item: JSONObject){
        val type = item.optString("type")
        HttpHelper.addCedEventAction(CedEventType.CLICK.CED_CLICK_USER_FEEDBACK_COPY,type + "：" + item.optString("value"))
        // 获取剪贴板管理器
        val clipboardManager = mvpView?.getContext()?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个 ClipData 对象，将要复制的文本放入其中
        val clipData = ClipData.newPlainText("label", item.optString("value"))
        // 将 ClipData 对象放入剪贴板
        clipboardManager.setPrimaryClip(clipData)
        mvpView?.showCedToast(MyApplication.app.getString(R.string.kefu_contact_copy_success))
    }

    /**
     * 拨号
     */
    fun onCedCallAction(item: JSONObject){
        val type = item.optString("type")
        HttpHelper.addCedEventAction(CedEventType.CLICK.CED_CLICK_USER_FEEDBACK_DIAL,type + "：" + item.optString("value"))
        val callIntent = Intent(Intent.ACTION_DIAL)
        callIntent.setData(Uri.parse("tel:${item.optString("value")}"))
        mvpView?.getContext()?.startActivity(callIntent)
    }

    /**
     * 清除用户数据 或 获取是否显示删除数据按钮
     */
    fun checkClearUserDataAction(isShowButton: Boolean){
        userModel?.clearUserDataAction(isShowButton, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")){
                    0 -> {//成功
                        if (isShowButton){
                            val obj = data.optJSONObject("obj")
                            if (obj != null && obj.has("showDropButton") && obj.optString("showDropButton") == "YES"){
                                mvpView?.showCedClearUserDataButton(true)
                            } else {
                                mvpView?.showCedClearUserDataButton(false)
                            }
                        } else {
                            mvpView?.showCedClearUserDataSuccess()
                        }
                    }
                    else -> {//失败
                        mvpView?.showCedToast(data.optString("msg"))
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }
            override fun onRequestFinish() {
            }
        })
    }
}